<script setup>
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import BooksList from "@/components/BooksList.vue";
import axios from "axios";

import { useRoute } from "vue-router";
const goBack = () => {
  router.go(-1); 
};
const route = useRoute();
// // 1.获取路由参数cate
let cate = ref(route.query.cate);
const router = useRouter();
// 搜索的关键词
let skey = ref("");
// 随机推荐的书籍名称
let randomBookNames = ref([]);

// 搜索
const doSearch = () => {
  if (!skey.value.trim()) {
    alert("请输入搜索关键词");
    return;
  }
  
  // 跳转到搜索结果页面
  router.push({
    path: '/search',
    query: { keyword: skey.value }
  });
};

// 获取随机书籍名称
const getRandomBookNames = async () => {
  try {
    const response = await axios.get('/api/randomBooks?count=3');
    if (response.data.code === 200) {
      // 提取书名
      randomBookNames.value = response.data.data.map(book => book.bookname);
    }
  } catch (error) {
    console.error("获取随机书籍名称失败:", error);
    // 如果API调用失败，使用默认值
    randomBookNames.value = ["拓荒的夏娃", "猫咪海洋简史", "橙黄橘绿半甜时"];
  }
};

// 检查URL参数中是否有搜索关键词，如果有则自动搜索
onMounted(() => {
  if (route.query.skey) {
    skey.value = route.query.skey;
  }
  
  // 获取随机书籍名称
  getRandomBookNames();
});
</script>

<template>
  <!-- header -->
  <div class="searchBar">
    <div class="clearfix">
      <!--logo-->
      <div class="logo">
        <img src="\src\assets\logo3.png" />
      </div>
      <!--搜索区域-->
      <div class="searchArea">
        <div class="searchFrom clearfix">
          <span class="inputWrap">
            <input
              type="text"
              id="keyword"
              name="keyword"
              placeholder="人间值得"
              v-model="skey"
              @keyup.enter="doSearch"
            />
          </span>
          <a href="#" class="searchBut" @click.prevent="doSearch">搜索</a>
        </div>
        <!--  -->
        <div class="hotWord">
          <a v-for="(bookName, index) in randomBookNames" :key="index" href="#" @click.prevent="skey = bookName; doSearch()">
            {{ bookName }}
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 头部导航 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.searchBar {
  position: relative;
  height: 100px;
  z-index: 9;
  background: #fff;
  width: 1200px;
  margin: auto;
}
.searchBar .logo {
  float: left;
  width: 150px;
  margin-right: 26px;
}
.logo img {
  width: 100%;
}
.searchArea {
  float: left;
  margin-top: 20px;
  margin-left: 100px;
  max-width: 715px;
}
.clearfix {
  zoom: 1;
  display: block;
}
.searchFrom {
  float: left;
  height: 36px;
  width: 606px;
  border: 2px solid #e60000;
  position: relative;
}
.inputWrap {
  padding: 8px 0;
  float: left;
  width: 508px;
  right: 100px;
}
.inputWrap input {
  display: block;
  width: 100%;
  height: 20px;
  line-height: 20px;
  background: #fff;
  font-size: 12px;
  color: #666;
  text-indent: 1em;
  border: none;
}
.searchBut {
  /* float: right; */
  height: 36px;
  background: #e60000;
  color: #fff;
  line-height: 36px;
  width: 98px;
  text-align: center;
  font-size: 14px;
  font-family: "Microsoft YaHei";
  text-decoration: none;
  letter-spacing: 3px;
  position: absolute;
  top: -2px;
}

.searchArea .hotWord {
  float: left;
  line-height: 28px;
  overflow: hidden;
}
.searchArea .hotWord a {
  float: left;
  color: #999;
  margin-right: 23px;
  text-decoration: none;
}
.searchArea .hotWord a:hover{
  color: #e60000;
}
/* //////////////////////////////////////////////////////////// */

</style>
